import smartfetch from 'smartfetch'
import { Storage } from '@/utils'
import { getMenuAll } from '@/api/common'
const token = Storage('TOKEN') || ''
const userInfo = Storage('USERINFO') || {}

export default {
	state: {
		isLogin: Boolean(token),
		token,
		userInfo,
		menuAll:[]
	},
	mutations: {
		LOGIN_IN:(state, { token, userInfo}) => {
			state.token = token
			state.userInfo = userInfo
			smartfetch.modifyBaseConfigs(baseConfig => {
        baseConfig.forEach(item => {
          !item.headers && (item.headers = {})
          item.headers.token = token
        })
      })
			state.isLogin = Boolean(token)
      Storage('TOKEN', token)
      Storage('USERINFO', userInfo)
		},
		// 登出
		LOGIN_OUT: state => {
      state.isLogin = false
      state.token = ''
      state.userInfo = {}
      Storage(false)
    },
		SET_MENU_ALL: (state, menuAll) => {
			state.menuAll = menuAll;
		}
	},
	actions: {
		//获取全部菜单
		GetMenuAll({ commit }) {
			let that = this
			return new Promise(resolve => {
				that.$fetch(getMenuAll).done(data=>{
					commit('SET_MENU_ALL', data);
					resolve(data);
				})
			})
		}
	}
}